﻿using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Web;
using Agile.Paypal.Web;

namespace Agile.Paypal
{
    /// <summary>
    /// Class PaypalRedirectionBuilder
    /// </summary>
    public class PaypalRedirectionBuilder
    {


        private readonly Merchant _commerce;

        public PaypalRedirectionBuilder(Merchant commerce) {
            _commerce = commerce;
        }


        /// <summary>
        /// Creates the HTML for.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="writer">The writer.</param>
        public void CreateHtmlFor(PaypalBuyRequest request, TextWriter writer)
        {
            writer.WriteLine("<html>");
            writer.WriteLine("<head>");
            writer.WriteLine("</head>");

            writer.WriteLine(_commerce.TestEnviroment
                                 ? "<body>" : "<body onload=\"document.getElementById('theForm').submit();\">");


            CreateFormFor(request, writer);


            writer.WriteLine("</body>");
            writer.WriteLine("</html>");
        }

        /// <summary>
        /// Creates the form for.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="writer">The writer.</param>
        public void CreateFormFor(PaypalSubscriptionRequest request, TextWriter writer)
        {
            string url = !_commerce.TestEnviroment ? "https://www.paypal.com/cgi-bin/webscr" : "https://www.sandbox.paypal.com/cgi-bin/webscr";



            writer.WriteLine("<form action=\"{0}\" method=\"post\">", url);

            writer.WriteLine("<INPUT TYPE=\"hidden\" name=\"{0}\" value=\"{1}\" />", "cmd", "_xclick-subscriptions");
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "a3", decimal.Round(request.Amount, 2).ToString(CultureInfo.InvariantCulture));
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "p3", 1);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "t3", request.Frequency);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "src", "1");
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "srt", request.Recurrences);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "no_note", "1");

            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "no_shipping", 1);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "rm", 2);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "business", _commerce.Email);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "item_name", request.ProductName);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "currency_code", "EUR");

            //writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "notify_url", GetNotificationUrl());
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "custom", request.CustomData);
            writer.WriteLine("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", "return", request.OnSuccess);
            writer.WriteLine(Localization.IfNotRedirectedClick);
            writer.WriteLine("<input type=\"submit\" value=\""+Localization.Here+"\">");
            writer.WriteLine("</form>");

        }

        /// <summary>
        /// Creates the HTML for.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="writer">The writer.</param>
        public void CreateHtmlFor(PaypalSubscriptionRequest request, TextWriter writer)
        {
            writer.WriteLine("<html>");
            writer.WriteLine("<head>");
            writer.WriteLine("</head>");

            writer.WriteLine(_commerce.TestEnviroment
                                 ? "<body>" : "<body onload=\"document.getElementById('theForm').submit();\">");

            CreateFormFor(request, writer);

            writer.WriteLine("</body>");
            writer.WriteLine("</html>");
        }

    }
}
